/**
 * useReducer(减少组件层级)
 */
 import React, {useReducer } from 'react';
 
 //处理函数
 const reducer=(pervState,action)=>{    //第一个参数是原数据,第二个参数是传来的数据
    let newarr={...pervState};//定义新对象拷贝
    switch(action.type){
        case "pengke-jian":
            newarr.count--
            return newarr        
        case "pengke-jia":
            newarr.count++
            return newarr
        default:
            return pervState   
    }
 }
 //外部的对象
 const intialState={
     count:0,
 }
 export default function MyReducer() {
    const [state,dispatch]=useReducer(reducer,intialState)
    return (
        <div>
            <button onClick={()=>{
                dispatch({
                    type:"pengke-jian"
                })
            }}>-</button>
            <span>{state.count}</span>
            <button onClick={()=>{
                dispatch({
                    type:"pengke-jia"
                })
            }}>+</button>
        </div>   
    )
 }